package com.mimiwang.comment.controller;

import com.github.pagehelper.PageInfo;
import com.mimiwang.comment.entity.Comment;
import com.mimiwang.comment.service.AnimalCommentService;
import com.mimiwang.comment.vo.CommentVo;
import com.mimiwang.common.utils.ResponseUtil;
import com.mimiwang.common.vo.school.PageConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @AUTHOR:游李滨
 * @DATE:2023/5/15 16:51
 */

@RestController
@RequestMapping("/comment")
public class AnimalCommentController {

    @Autowired
    AnimalCommentService animalCommentService;


    @GetMapping("/ani/list/{id}")
    public String getAllCommentByAId(@PathVariable("id") long animalId, PageConfig pageConfig){
        if(animalId == 0){
            return ResponseUtil.errorJSONData("动物id错误");
        }

        PageInfo<CommentVo> commentVoPageInfo = animalCommentService.selectCommentListByAnimalId(animalId, pageConfig);
        return ResponseUtil.successJSONData(commentVoPageInfo);
    }
    @GetMapping("/ani/list/child/{id}")
    public String getChildCommentByPId(@PathVariable("id") long parentId, PageConfig pageConfig){
        if(parentId == 0){
            return ResponseUtil.errorJSONData("父评论id错误");
        }

        PageInfo<CommentVo> commentVoPageInfo = animalCommentService.selectChildCommentListByParentId(parentId, pageConfig);

        return ResponseUtil.successJSONData(commentVoPageInfo);
    }
    /**
     *  发送者id，发送内容content，动物id
     *
     * */

    @PostMapping("/ani/add")
    public String addComment(@RequestBody Comment comment,int animalId){
        if(comment.getSenderId() == 0){
            return ResponseUtil.errorJSONData("id错误");
        }

        boolean b = animalCommentService.insertAnimalComment(comment, animalId);
        if(!b){
            return ResponseUtil.errorJSONData("评论失败");
        }
        return ResponseUtil.successJSONData("评论成功",comment);
    }

    /**
     *  父评论id，发送者id，发送内容content，发送层次level
     *
     * */
    @PostMapping("/ani/reply")
    public String replyComment(@RequestBody Comment comment){
        if(comment.getSenderId() == 0){
            return ResponseUtil.errorJSONData("id错误");
        }

        boolean b =animalCommentService.insertReplyComment(comment);
        if(!b){
            return ResponseUtil.errorJSONData("评论失败");
        }
        return ResponseUtil.successJSONData("评论成功",comment);
    }
}
